package m.html.form;

import m.html.element.*;
import m.html.element.attr.InputType;
import m.html.element.attr.Size;
import m.html.element.other.IElement;
import m.html.element.other.InputElement;
import m.system.util.StringUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 表单输入项
 */
public class FormInput extends AbstractFormItem<FormInput>{
    private FormInput(){}
    private InputElement input;//输入类型
    private Map<String,String> param;//非输入型参数
    private String description;//输入组件描述
    private boolean edit=true;//是否可编辑

    @Override
    protected IElement getItemElement() {
        Element div;
        if(StringUtil.isSpace(description)){
            div=Element.tag("div");
        }else{
            div=Element.tag("a-tooltip")
                .E(Element.tag("template").A("#title").E(this.description));
        }
        IElement ele=null;
        if(null!=input){
            if(!edit) ele=this.input.toViewElement();
            else ele=this.input;
        } else {
            String type = param.get("_type");
            if ("selectImage".equals(type)) {
                String model = param.get("model"), field = param.get("field"), imageType = param.get("imageType"), disabled = param.get("disabled");
                ele= Element.tag("a-space")
                    .E(
                        Element.tag("span").A("v-if", disabled),
                        edit?FormHtml.getSelectButton("selectImagePage(" + model + ",'" + field + "','" + imageType + "')").A("v-else"):null,
                        Element.tag("img").A("v-if", model + "." + field + ".thumPath").A(":src", model + "." + field + ".thumPath").A("style", "max-height:34px;")
                                .setExtend("@click=\"viewImage(" + model + "." + field + ")\""),
                        Element.tag("span").A("v-if", disabled),
                        edit?FormHtml.getClearButton(model + "." + field + "={}").A("v-else-if", model + "." + field + ".thumPath"):null

                    );
            } else if ("selectIcon".equals(type)) {
                String model = param.get("model"), field = param.get("field"), disabled = param.get("disabled"), business = param.get("business");
                ele= Element.tag("a-space")
                    .E(
                        Element.tag("span").A("v-if", disabled),
                        edit?FormHtml.getSelectButton("selectIconPage(" + model + ",'" + field + "','','"+business+"')").A("v-else"):null,
                        Element.tag("img").A("v-if", model + "." + field + ".path").A(":src", model + "." + field + ".path").A("style", "max-height:34px;")
                                .setExtend("@click=\"viewIcon(" + model + "." + field + ")\""),
                        Element.tag("span").A("v-if", disabled),
                        edit?FormHtml.getClearButton(model + "." + field + "={}").A("v-else-if", model + "." + field + ".path"):null
                    );
            } else if ("selectJsPage".equals(type) || "selectOneModel".equals(type) || "selectManyModel".equals(type)) {
                String model = param.get("model"), field = param.get("field"), url = param.get("url"), width = param.get("width"), html = param.get("html"), disabled = param.get("disabled");
                if (StringUtil.isSpace(url)) url = "''";
                if(!url.startsWith("'")) url="'"+url+"'";
                ele= Element.tag("a-space")
                    .E(
                        Element.tag("span").A("v-if", disabled),
                        edit?FormHtml.getSelectButton(type + "(" + model + ",'" + field + "'," + url + "," + width + ")").A("v-else"):null,
                        Element.tag("span").E(new Text(html))
                    );
            } else if ("selectOneAdmin".equals(type) || "selectManyAdmin".equals(type)){
                String model = param.get("model"), field = param.get("field"), org = param.get("org"), role = param.get("role"), html = param.get("html"), disabled = param.get("disabled");
                ele= Element.tag("a-space")
                    .E(
                        Element.tag("span").A("v-if", disabled),
                        edit?FormHtml.getSelectButton(type + "(" + model + ",'" + field + "'," + org + "," + role + ")").A("v-else"):null,
                        Element.tag("span").E(new Text(html))
                    );
            }
        }
        return div.E(ele);
    }

    /**
     * 输入框
     */
    public static FormInput input(String title,String field) {
        FormInput item=new FormInput();
        item.input=new Input(field, InputType.TEXT);
        item.setTitle(new Text(title));
        return item;
    }
    /**
     * 文本域
     */
    public static FormInput textarea(String title,String field) {
        FormInput item=new FormInput();
        item.input=new Input(field, InputType.TEXTAREA);
        item.setTitle(new Text(title));
        return item;
    }
    /**
     * 密码框
     */
    public static FormInput password(String title,String field) {
        FormInput item=new FormInput();
        item.input=new Input(field, InputType.PASSWORD);
        item.setTitle(new Text(title));
        return item;
    }
    /**
     * 整型框
     */
    public static FormInput integer(String title,String field){
        return integer(title,field,"");
    }
    /**
     * 整型框
     */
    public static FormInput integer(String title,String field,String suffix){
        return number(title,field,suffix,"#,##0",0);
    }
    /**
     * 数字框
     */
    public static FormInput number(String title,String field){
        return number(title,field,null);
    }
    /**
     * 数字框
     */
    public static FormInput number(String title,String field,String suffix){
        return number(title,field,suffix,"#,##0.00",2);
    }
    /**
     * 数字框
     */
    public static FormInput number(String title,String field,String viewFormat,int precision){
        return number(title,field,"",viewFormat,precision);
    }
    /**
     * 数字框
     */
    public static FormInput number(String title,String field,String suffix,String viewFormat,int precision){
        FormInput item=new FormInput();
        item.input=new InputNumber(field,suffix,viewFormat,precision);
        item.setTitle(new Text(title));
        return item;
    }
    /**
     * 多选框
     */
    public static FormInput checkBox(String title,String field){
        FormInput item=new FormInput();
        item.input=new CheckBox(field);
        item.setTitle(new Text(title));
        return item;
    }

    /**
     * 开关组件
     */
    public static FormInput switchBox(String title,String field,String checkedValue,String unCheckedValue){
        FormInput item=new FormInput();
        item.input=new Switch(field,checkedValue,unCheckedValue);
        item.setTitle(new Text(title));
        return item;
    }

    /**
     * 日期
     */
    public static FormInput date(String title,String field){
        FormInput item=new FormInput();
        item.input=new DatePicker(field);
        item.setTitle(new Text(title));
        return item;
    }

    /**
     * 日期时间
     */
    public static FormInput dateTime(String title,String field){
        FormInput item=new FormInput();
        item.input=new DatePicker(field,true);
        item.setTitle(new Text(title));
        return item;
    }

    /**
     * 选择框
     */
    public static FormInput select(String title,String field,String dict){
        FormInput item=new FormInput();
        item.input=new Select(field,dict);
        item.setTitle(new Text(title));
        return item;
    }

    /**
     * 单项项
     */
    public static FormInput radio(String title,String field,String dict){
        FormInput item=new FormInput();
        item.input=new Radio(field,dict);
        item.setTitle(new Text(title));
        return item;
    }
    /**
     * 多选项
     */
    public static FormInput checkBoxGroup(String title,String field,String dict){
        FormInput item=new FormInput();
        item.input=new CheckBoxGroup(field,dict);
        item.setTitle(new Text(title));
        return item;
    }

    /**
     * 选择项组件
     */
    public static FormInput selectItem(String title,String field,String dict){
        return selectItem(title,field,dict,false);
    }

    /**
     * 选择项组件
     */
    public static FormInput selectItem(String title,String field,String dict,boolean multiple){
        FormInput item=new FormInput();
        item.input=new SelectItem(field,dict,multiple);
        item.setTitle(new Text(title));
        return item;
    }
    /**
     * 选择树组件
     */
    public static FormInput selectTree(String title,String field,String dict){
        return selectTree(title,field,dict,false,false);
    }
    /**
     * 选择树组件
     */
    public static FormInput selectTree(String title,String field,String dict,boolean multiple){
        return selectTree(title,field,dict,multiple,false);
    }
    /**
     * 选择树组件
     */
    public static FormInput selectTree(String title,String field,String dict,boolean multiple,boolean parent){
        FormInput item=new FormInput();
        item.input=new SelectTree(field,dict,multiple,parent);
        item.setTitle(new Text(title));
        return item;
    }
    /**
     * 选择图片
     */
    public static FormInput selectImage(String title,String model,String field,String imageType){
        FormInput item=new FormInput();
        item.setTitle(new Text(title));
        item.param=new HashMap<>();
        item.param.put("_type","selectImage");
        item.param.put("model",model);
        item.param.put("field",field);
        item.param.put("disabled","false");
        item.param.put("imageType",imageType);
        return item;
    }
    /**
     * 选择图标
     */
    public static FormInput selectIcon(String title,String model,String field){
        return selectIcon(title,model,field,"B");
    }
    /**
     * 选择图标  A系统 B业务
     */
    public static FormInput selectIcon(String title,String model,String field,String business){
        FormInput item=new FormInput();
        item.setTitle(new Text(title));
        item.param=new HashMap<>();
        item.param.put("_type","selectIcon");
        item.param.put("model",model);
        item.param.put("field",field);
        item.param.put("disabled","false");
        item.param.put("business",business);
        return item;
    }

    public static FormInput selectOneModel(String title,String model,String field,String url,String html){
        return selectOneModel(title,model,field,url,1000,html);
    }
    /**
     * 选择js页面
     * url 需用''拼接
     */
    public static FormInput selectJsPage(String title,String model,String field,String url,int width,String html){
        FormInput item=new FormInput();
        item.setTitle(new Text(title));
        item.param=new HashMap<>();
        item.param.put("_type","selectJsPage");
        item.param.put("model",model);
        item.param.put("field",field);
        item.param.put("disabled","false");
        item.param.put("url",url);
        item.param.put("width",String.valueOf(width));
        item.param.put("html",html);
        return item;
    }
    /**
     * 选择一个model
     * url 需用''拼接
     */
    public static FormInput selectOneModel(String title,String model,String field,String url,int width,String html){
        FormInput item=new FormInput();
        item.setTitle(new Text(title));
        item.param=new HashMap<>();
        item.param.put("_type","selectOneModel");
        item.param.put("model",model);
        item.param.put("field",field);
        item.param.put("disabled","false");
        item.param.put("url",url);
        item.param.put("width",String.valueOf(width));
        item.param.put("html",html);
        return item;
    }
    /**
     * 选择多个model
     * url 需用''拼接
     */
    public static FormInput selectManyModel(String title,String model,String field,String url,String html){
        FormInput item=selectOneModel(title,model,field,url,html);
        item.param.put("_type","selectManyModel");
        return item;
    }

    /**
     * 选择一个管理员
     */
    public static FormInput selectOneAdmin(String title,String model,String field,String org,String role,String html){
        FormInput item=new FormInput();
        item.setTitle(new Text(title));
        item.param=new HashMap<>();
        item.param.put("_type","selectOneAdmin");
        item.param.put("model",model);
        item.param.put("field",field);
        item.param.put("disabled","false");
        item.param.put("org",org);
        item.param.put("role",role);
        item.param.put("html",html);
        return item;
    }
    /**
     * 选择多个管理员
     */
    public static FormInput selectManyAdmin(String title,String model,String field,String org,String role,String html){
        FormInput item=selectOneAdmin(title,model,field,org,role,html);
        item.param.put("_type","selectManyAdmin");
        return item;
    }
    /**
     * 设置尺寸
     */
    public FormInput setSize(Size size) {
        if(null==input){
            this.param.put("size",size.toString());
        }else {
            input.setSize(size);
        }
        return this;
    }

    /** 提示文字
     * 固定汉字
     */
    public FormInput setPlaceholder(String placeholder) {
        String[] arr=StringUtil.noSpace(placeholder).split("\\|");
        if(null==input){
            this.param.put("placeholder",arr[0]);
        }else {
            if (input instanceof CheckBox) {
                input.E(new Text(arr[0]));
            } else {
                input.setPlaceholder(arr[0]);
            }
        }
        if(arr.length>1) this.setDescription(arr[1]);
        return this;
    }

    /** 是否禁用
     * 表达式
     */
    public FormInput setDisabled(String disabled) {
        if(null==input){
            this.param.put("disabled",disabled);
        }else {
            input.setDisabled(disabled);
        }
        return this;
    }

    /**
     * 设置输入组件描述
     */
    public FormInput setDescription(String description){
        this.description=description;
        return this;
    }

    /**
     * 设置是否可编辑
     */
    public FormInput setEdit(boolean edit) {
        this.edit = edit;
        return this;
    }
}
